﻿using System;
using System.Web;
using System.Web.Mvc;
using sBlog.Net.Common;
using System.Collections.Generic;
using sBlog.Net.Domain.Entities;
using sBlog.Net.Domain.Interfaces;

namespace sBlog.Net.Areas.Admin.Controllers
{
    public class VideoController : Controller
    {
        private readonly IVideo _videoRepository;

        public VideoController(IVideo videoRepository)
        {
            _videoRepository = videoRepository;
        }

        public ActionResult Index()
        {
            int total = 0;
            List<VideoEntity> lst = _videoRepository.GetListVideoWithPage(1, 25, ref total);
            return View(lst);
        }

        public ActionResult Add()
        {
            VideoEntity videoInfo = new VideoEntity();
            return View(videoInfo);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(VideoEntity videoInfo)
        {
            if (ModelState.IsValid)
            {
                videoInfo.CreateDate = DateTime.Now;
                videoInfo.UpdateDate = DateTime.Now;
                videoInfo.Folder = DateTime.Now.Year.ToString("d4") + DateTime.Now.Month.ToString("d2");
                string videoFolder = videoInfo.VideoName.Substring(0, videoInfo.VideoName.Length - 4);
                videoInfo.Links = "http://mlog-media.yan.vn/MlogVideo/" + videoInfo.Folder.Trim() + "/" + videoFolder.Trim() + "/" + videoFolder.Trim() + ".m3u8";
                _videoRepository.AddVideo(videoInfo);
            }
            return RedirectToAction("Index", "Video");
        }

        public ActionResult Edit(int id)
        {
            VideoEntity videoInfo = _videoRepository.GetById(id);
            ViewData["VideoName"] = videoInfo.VideoName;
            ViewData["Folder"] = videoInfo.Folder;
            return View(videoInfo);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(VideoEntity videoInfo, string Folder, string VideoName)
        {
            if (ModelState.IsValid)
            {
                videoInfo.UpdateDate = DateTime.Now;
                videoInfo.Folder = Folder;
                videoInfo.VideoName = VideoName;
                string videoFolder = videoInfo.VideoName.Substring(0, videoInfo.VideoName.Length - 4);
                videoInfo.Links = "http://mlog-media.yan.vn/MlogVideo/" + videoInfo.Folder.Trim() + "/" + videoFolder.Trim() + "/" + videoFolder.Trim() + ".m3u8";
                _videoRepository.UpdateVideo(videoInfo);
            }

            return RedirectToAction("Index", "Video");
        }

        public string UploadVideo(HttpPostedFileBase fileData)
        {
            string fName = Guid.NewGuid().ToString() + ".xyz";
            string strUserNameFtp = System.Configuration.ConfigurationManager.AppSettings["ftpUserNameVideo"].ToString();
            string strPassFtp = System.Configuration.ConfigurationManager.AppSettings["ftpPasswordVideo"].ToString();
            string ftpServer = System.Configuration.ConfigurationManager.AppSettings["ftpServerVideo"].ToString();

            string strFtpAddress = ftpServer + "/MlogVideo";

            string strFolderUser = ImageUtility.GetFolderByDate(strFtpAddress);
            ImageUtility.FtpCreateFolder(strFolderUser, strUserNameFtp, strPassFtp);

            string ftpUploadUrl = strFolderUser + "/" + fName;
            ImageUtility.UploadVideoClip(ftpUploadUrl, fileData, fName, strUserNameFtp, strPassFtp);
            fName = fName.Replace("xyz", "mp4");
            string strDate = DateTime.Now.Year.ToString() + DateTime.Now.ToString("MM");
            return strDate + fName;
        }
    }
}
